package net.thxjj.adminServer.service.impl;

import net.thxjj.adminServer.entity.*;
import net.thxjj.adminServer.entity.aep.DevLog;
import net.thxjj.adminServer.entity.aep.SmokeData;
import net.thxjj.adminServer.mongodao.MongoEventDao;
import net.thxjj.adminServer.mongodao.MongoLoginDao;
import net.thxjj.adminServer.mongodao.MongoLoginHandleDao;
import net.thxjj.adminServer.mongodao.MongoSmokeDao;
import net.thxjj.adminServer.service.IDevHistoryService;
import net.thxjj.adminServer.util.ParameterMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DevHistoryService implements IDevHistoryService {

    @Autowired
    private MongoSmokeDao mongoSmokeDao;
    @Autowired
    private MongoLoginHandleDao mongoLoginHandleDao;
    @Autowired
    private MongoLoginDao mongoLoginDao;
    @Autowired
    private MongoEventDao mongoEventDao;

    @Override
    public List<List<Object>> getSmokeValueList(String device_id,long start_time,long end_time) {

        List<SmokeData> list = mongoEventDao.getSmokeValueList(device_id,start_time,end_time);

        List<List<Object>> listcur = new ArrayList<>();
        for (SmokeData map:list) {
            long value = map.getSmoke_value();
            long timestamp = map.getTimestamp();
            List<Object> lista = new ArrayList<>();
            lista.add(timestamp);
            lista.add(value);
            listcur.add(lista);
        }

        return listcur;
    }

    @Override
    public List<List<Object>> getBatteryValueList(String device_id, long start_time, long end_time) {
        List<SmokeData> list = mongoEventDao.getBatteryValueList(device_id,start_time,end_time);

        List<List<Object>> listbattery = new ArrayList<>();
        for (SmokeData map:list) {

            long value = map.getBattery_value();
            long timestamp = map.getTimestamp();
            List<Object> lista = new ArrayList<>();
            lista.add(timestamp);
            lista.add(value);
            listbattery.add(lista);
        }

        return listbattery;
    }


    @Override
    public List<List<Object>> getLoginErrList(String device_id) {
       List<LoginHadleEvent> list =  mongoLoginDao.getLoginHandleWithLog(device_id);
        List<List<Object>> listLogin = new ArrayList<>();
        for (LoginHadleEvent map:list) {
            long ad = map.getUserId();
            long value = 1;
                if(ad>0){
                    value = 2;
                }
            long timestamp = map.getTimestamp();
            List<Object> lista = new ArrayList<>();
            lista.add(timestamp);
            lista.add(value);
            listLogin.add(lista);
        }
        return listLogin;
    }

    @Override
    public HashMap<String, Object> handleLoginErr(String device_id, long userId) {

        try {
            LoginHadleEvent loginHadleEvent = new LoginHadleEvent();
            loginHadleEvent.setDeviceId(device_id);
            loginHadleEvent.setUserId(userId);
            loginHadleEvent.setTimestamp(System.currentTimeMillis());
            mongoLoginHandleDao.saveLoginHadleEvent(loginHadleEvent);

            return ResponseModel.getModel(ResultEnum.SUCCESS,null);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseModel.getModel(ResultEnum.FAILED,null);
        }

    }

    @Override
    public List<ParameterMap> smokeHappenList(String device_id, long start_time, long end_time) {

        List<ParameterMap> listtotal = new ArrayList<>();

        DevLog devLog_smoke = mongoEventDao.getNewstEventByTime(device_id,start_time,"\"smoke_state\":0");
        DevLog devLog_smoke_normal = mongoEventDao.getNewstEventByTime(device_id,start_time,"\"smoke_state\":1");
        SmokeData smokeData = mongoSmokeDao.getNewestSmokeAlarmByTime(device_id,start_time);
        SmokeData smokeData_nor = mongoSmokeDao.getNewestSmokeNormalByTime(device_id,start_time);

        long devLog_smoke_time = devLog_smoke==null?0:devLog_smoke.getCreateTime();
        long devLog_smoke_normal_time = devLog_smoke_normal==null?0:devLog_smoke_normal.getCreateTime();
        long smokeData_time = smokeData==null?0:smokeData.getTimestamp();
        long smokeData_nor_time = smokeData_nor==null?0:smokeData_nor.getTimestamp();
        long smoke_time = Math.max(devLog_smoke_time,smokeData_time);
        long smoke_time_nor = Math.max(devLog_smoke_normal_time,smokeData_nor_time);
        List<SmokeData> list;
        if(smoke_time>smoke_time_nor){
            list = mongoEventDao.getSmokeHappenList(device_id,smoke_time-1,end_time);
        }else {
            list = mongoEventDao.getSmokeHappenList(device_id,start_time,end_time);
        }

        if(list.size()>1){
            long start_smoke_time_init = list.get(0).getTimestamp();
            for(int i=0;i<list.size()-1;i++){
                long start_smoke_time = list.get(i).getTimestamp();
                long end_smoke_time = list.get(i+1).getTimestamp();
                long end_smoke_time_2 = mongoEventDao.getSmokeRecoryPoint(device_id,start_smoke_time,end_smoke_time);
                if(end_smoke_time_2!=0){
                    ParameterMap parameterMap = new ParameterMap();
                    parameterMap.put("start_time",start_smoke_time_init);
                    parameterMap.put("end_time",end_smoke_time_2);
                    listtotal.add(parameterMap);
                    start_smoke_time_init = end_smoke_time;
                }
            }

            long end_smoke_time_e = mongoEventDao.getSmokeRecoryPoint(device_id,list.get(list.size()-1).getTimestamp(),end_time);
            if(end_smoke_time_e!=0){
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",start_smoke_time_init);
                parameterMap.put("end_time",end_smoke_time_e);
                listtotal.add(parameterMap);
            }else{
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",start_smoke_time_init);
                parameterMap.put("end_time",-1);
                listtotal.add(parameterMap);
            }

        } else if(list.size()==1){
            long end_smoke_time = mongoEventDao.getSmokeRecoryPoint(device_id,list.get(0).getTimestamp(),end_time);
            if(end_smoke_time!=0){
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",list.get(0).getTimestamp());
                parameterMap.put("end_time",end_smoke_time);
                listtotal.add(parameterMap);
            }else{
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",list.get(0).getTimestamp());
                parameterMap.put("end_time",-1);
                listtotal.add(parameterMap);
            }
        }

        return listtotal;
    }


    //type = 1代表烟感故障，2代表低电压，3代表拆除
    @Override
    public List<ParameterMap> faultHappenList(String device_id, long start_time, long end_time,int type) {
        String fault_str = "";
        String fault_str_nor = "";
        if(type==1){
            fault_str = "\"error\":1";
            fault_str_nor = "\"error\":2";
        }else if(type == 2){
            fault_str = "\"error\":3";
            fault_str_nor = "\"error\":4";
        }else if(type == 3){
            fault_str = "\"tamper_alarm\":1";
            fault_str_nor = "\"tamper_alarm\":0";
        }

        List<ParameterMap> listtotal = new ArrayList<>();

        DevLog devLog = mongoEventDao.getNewstEventByTime(device_id,start_time,fault_str);

        DevLog devLog_nor = mongoEventDao.getNewstEventByTime(device_id,start_time,fault_str_nor);

        if(devLog!=null){
            if(devLog_nor==null){
                start_time = devLog.getCreateTime()-1;
            }else{
               if(devLog.getCreateTime()>devLog_nor.getCreateTime()){
                   start_time = devLog.getCreateTime()-1;
               }
            }
        }

       List<DevLog> list = mongoEventDao.getEventAtTime(device_id,start_time,end_time,fault_str);

        if(list.size()>1){
            long start_fault_time_init = list.get(0).getCreateTime();
            for(int i=0;i<list.size()-1;i++){
                long start_fault_time = list.get(i).getCreateTime();
                long end_fault_time = list.get(i+1).getCreateTime();
                DevLog end_fault_time_2 = mongoEventDao.getEarliestEventAtTime(device_id,start_fault_time,end_fault_time,fault_str_nor);
                if(end_fault_time_2!=null){
                    ParameterMap parameterMap = new ParameterMap();
                    parameterMap.put("start_time",start_fault_time_init);
                    parameterMap.put("end_time",end_fault_time_2.getCreateTime());
                    listtotal.add(parameterMap);
                    start_fault_time_init = end_fault_time;
                }
            }

            DevLog end_fault_time_e = mongoEventDao.getEarliestEventAtTime(device_id,list.get(list.size()-1).getCreateTime(),end_time,fault_str_nor);
            if(end_fault_time_e!=null){
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",start_fault_time_init);
                parameterMap.put("end_time",end_fault_time_e.getCreateTime());
                listtotal.add(parameterMap);
            }else{
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",start_fault_time_init);
                parameterMap.put("end_time",-1);
                listtotal.add(parameterMap);
            }

        } else if(list.size()==1){
            DevLog end_fault_time = mongoEventDao.getEarliestEventAtTime(device_id,list.get(0).getCreateTime(),end_time,fault_str_nor);
            if(end_fault_time!=null){
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",list.get(0).getCreateTime());
                parameterMap.put("end_time",end_fault_time.getCreateTime());
                listtotal.add(parameterMap);
            }else{
                ParameterMap parameterMap = new ParameterMap();
                parameterMap.put("start_time",list.get(0).getCreateTime());
                parameterMap.put("end_time",-1);
                listtotal.add(parameterMap);
            }
        }


        return listtotal;
    }



}
